package xcmg.device.web.company;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.company.CompanyAddressService;
import xcmg.device.service.vo.company.ReceiverAddressSearchParam;
import xcmg.device.service.vo.company.ReceiverAddressVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;
import yb.ecp.fast.infra.util.StringUtil;

import java.util.List;

@RestController
@RequestMapping("/companyAddress")
public class CompanyAddressController extends BasicController {

    @Autowired
    private CompanyAddressService companyAddressService;

    @RequestMapping(value = "/addAddress",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.InnerManage.ADDRESS_ADD )
    public ActionResult addAddress(@RequestBody ReceiverAddressVO receiverAddressVO) throws Exception
    {
        receiverAddressVO.setOrgId(getOrgId());
        ErrorCode code=companyAddressService.addAddress(receiverAddressVO);
        return  actionResult(code);
    }

    @RequestMapping(value = "/updateAddress",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.InnerManage.ADDRESS_ADD )
    public ActionResult updateAddress(@RequestBody ReceiverAddressVO receiverAddressVO)
    {
        ErrorCode code=companyAddressService.updateAddress(receiverAddressVO);
        return  actionResult(code);
    }

    @RequestMapping(value = "/deleteAddress",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.InnerManage.ADDRESS_ADD )
    public ActionResult deleteAddress(@RequestBody ReceiverAddressVO receiverAddressVO)
    {
        ErrorCode code=companyAddressService.deleteAddress(receiverAddressVO);
        return  actionResult(code);
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<ReceiverAddressSearchParam> condition) throws Exception
    {
        ReceiverAddressSearchParam receiverAddressSearchParam=condition.getFilters();
        if(receiverAddressSearchParam == null){
            receiverAddressSearchParam=new ReceiverAddressSearchParam();
        }
        receiverAddressSearchParam.setOrgId(getOrgId());
        condition.setFilters(receiverAddressSearchParam);
        PageCommonVO pageCommonVO=companyAddressService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id){
        return actionValueResult(companyAddressService.item(id));
    }

    @RequestMapping(value = "/receiverMatching",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult receiverMatching(ReceiverAddressSearchParam receiverAddressSearchParam)throws Exception{
        if(receiverAddressSearchParam==null || StringUtil.isNullOrEmpty(receiverAddressSearchParam.getDealerOrgId())){
            receiverAddressSearchParam.setOrgId(getOrgId());
        }else {
            receiverAddressSearchParam.setSupplierOrgId(getOrgId());
        }
        List<ReceiverAddressVO> result=companyAddressService.receiverMatching(receiverAddressSearchParam);
        return  actionValueResult(result);
    }
}
